package C016;

/**
 * ClassName: ArrayImple
 * Package: C016
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/3 16:42
 * @Version 1.0
 * @Since 1.0
 */
public class ArrayImple {
    public static void main(String[] args) {
        MyCircularDeque2 queue = new MyCircularDeque2(3);
        System.out.println(queue.insertLast(1));
        System.out.println(queue.insertLast(2));
        System.out.println(queue.insertFront(3));
        System.out.println(queue.insertFront(4));
        System.out.println(queue.getRear());
        System.out.println(queue.isFull());
        System.out.println(queue.deleteLast());
        System.out.println(queue.insertFront(4));
        System.out.println(queue.getFront());
    }
}

class MyCircularDeque {
    Integer[] array;
    int limit;
    int size, l, r;

    public boolean isFull() {
        return size == limit;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public MyCircularDeque(int k) {
        limit = k;
        array = new Integer[k];
    }

    public boolean insertFront(int value) {

        if (isFull()) {
            return false;
        }
        if (isEmpty()) {
            l = r = 0;
            array[r] = value;
            size++;
            return true;
        }
        r = r + 1 == limit ? 0 : r + 1;
        array[r] = value;
        size++;
        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        }
        if (isEmpty()) {
            l = r = 0;
            array[l] = value;
            size++;
            return true;
        }
        l = l - 1 == -1 ? limit - 1 : l - 1;
        array[l] = value;
        size++;
        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        }
        r = r - 1 == -1 ? limit - 1 : r - 1;
        size--;
        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        }
        l = l + 1 == limit ? 0 : l + 1;
        size--;
        return true;
    }

    public int getFront() {
        if (isEmpty()) {
            return -1;
        }
        return array[r];
    }

    public int getRear() {
        if (isEmpty()) {
            return -1;
        }
        return array[l];
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */

class MyCircularDeque2 {

    public int[] deque;
    public int l, r, size, limit;

    public MyCircularDeque2(int k) {
        deque = new int[k];
        l = r = size = 0;
        limit = k;
    }

    public boolean insertFront(int value) {
        if (isFull()) {
            return false;
        } else {
            if (isEmpty()) {
                l = r = 0;
                deque[0] = value;
            } else {
                l = l == 0 ? (limit - 1) : (l - 1);
                deque[l] = value;
            }
            size++;
            return true;
        }
    }

    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        } else {
            if (isEmpty()) {
                l = r = 0;
                deque[0] = value;
            } else {
                r = r == limit - 1 ? 0 : (r + 1);
                deque[r] = value;
            }
            size++;
            return true;
        }
    }

    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        } else {
            l = (l == limit - 1) ? 0 : (l + 1);
            size--;
            return true;
        }
    }

    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        } else {
            r = r == 0 ? (limit - 1) : (r - 1);
            size--;
            return true;
        }
    }

    public int getFront() {
        if (isEmpty()) {
            return -1;
        } else {
            return deque[l];
        }
    }

    public int getRear() {
        if (isEmpty()) {
            return -1;
        } else {
            return deque[r];
        }
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == limit;
    }

}
